package jdbc;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.swing.JOptionPane;

import businessEntities.Cours;
import businessEntities.DateCours;

import com.sun.swing.internal.plaf.synth.resources.synth;

public class Connexion {
	public static Connexion connexion;
	
	private Connection connect = null;
	
	private Connexion() throws ClassNotFoundException, SQLException{
		Class.forName("oracle.jdbc.OracleDriver" );
        connect = null;
	}
	
	public static Connexion getConnexion() throws ClassNotFoundException, SQLException{
		if(connexion==null)
			connexion=new Connexion();
		return connexion;
	}
	
	private void openConnexion() throws SQLException{
		connexion.connect= DriverManager.getConnection("jdbc:oracle:thin:@88.191.117.70:1521:xe", "miage1", "miage1" );
	}
	private void closeConnexion() throws SQLException{
		if(connexion.connect!=null)
			connexion.connect.close();
	}
	
	public synchronized static List<Object[]> executeSql(String sql,int nbColumn){
		List<Object[]> list = null;
		Connexion connect;
		System.out.println(sql);
		try {
			connect = Connexion.getConnexion();
			connect.openConnexion();
	        Statement stmt = connect.connect.createStatement();
	        ResultSet results = stmt.executeQuery(sql);
	        while(results.next()){
	        	if(list==null)list = new ArrayList<Object[]>();
	        	Object[] oTemp = new Object[nbColumn];
	        	for(int i=0;i<nbColumn;i++){
	        		oTemp[i] = results.getObject(i+1);
	        	}
	        	list.add(oTemp);
            }
	        connect.closeConnexion();
		} catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null, "Connexion Impossible " + e.getMessage());
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static void main(String[] args) {
		String isin = "QS0010040911";
		String sql = "SELECT " +
		"datecours, " +
		"isin, " +
		"valeurouverture, " +
		"valeurfermeture, " +
		"valeurhaut, " +
		"valeurbas, " +
		"volume " +
		"FROM " +
		"cours " +
		"WHERE " +
		"isin = '"+isin+"' ";
	
		List<Object[]> lOCours = Connexion.executeSql(sql, 7);
		HashMap<Date, Cours> lCours = new HashMap<Date, Cours>();
		for(Object[] oCours : lOCours){
			Cours cours = new Cours();
			Date date = DateCours.getDate(oCours[0]+"", DateCours.FORMAT_YYYYY_MM_DD_TIRE);
			
			cours.setDateCours(date);
			cours.setIsin(oCours[1]+"");
			cours.setOuverture(((BigDecimal)oCours[2]).doubleValue());
			cours.setFermeture(((BigDecimal)oCours[3]).doubleValue());
			cours.setHaut(((BigDecimal)oCours[4]).doubleValue());
			cours.setBas(((BigDecimal)oCours[5]).doubleValue());
	
			lCours.put(date,cours);
		}
	}
}
